import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import render from "@feidao/web/atom/browser/render/render";
import nodejs from "@feidao/web/atom/browser/msg/nodejs";
import P03 from './P03';
import P02 from './P02';
import P04 from './P04';
import P01 from './P01';

export default async function a011(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
	//  获取用户信息
	const f = {
		criteria: { staff_job_status: '1' },
		sort: {},
		page_size: 10,
		page_no: 1
	}

	const data = await nodejs(fd.data.spaceid, 'teach_staff_find', f);
	if (data.count == 0) {
		render(fd, '', P03, 'P03', 'inner');
	} else {
		render(fd, data, P02, 'P02', 'inner');
	}

	render(fd, data, P04, 'P04', 'inner');

	// 存放全局方法
	const condition = { param: {}, _param: {}, order: {} };
	fd.data.condition = condition;

	fd.data.refresh_data = async function (fd: IFeidaoAiBrowserComponent) {
		const condition = fd.data.condition;
		//刷新列表
		const data = await nodejs(fd.data.spaceid, "teach_staff_find", { criteria: condition.param, page_no: 1, page_size: 10, sort: condition.order });
		render(fd, data, P02, 'P02', 'inner');

		const page_node = fd.data.node.querySelector('[data-feidao-id="pages"]') as HTMLElement;
		fd.data.page_no = '1';
		page_node.setAttribute('page-no', fd.data.page_no);
		page_node.setAttribute('total', data.count);
	}

	fd.data.refresh_filter_data = async function (fd: IFeidaoAiBrowserComponent) {
		const name2title: any = {
			'staff_name': '员工姓名'
		};
		const condition = fd.data.condition;
		const order = condition.order;
		const param = condition._param;
		const data: any[] = [];
		if (Object.keys(order).length > 0) {
			const tpl_d = { field_name: "order" };
			const keys = Object.keys(order);
			const value: any[] = [];
			tpl_d['field_value'] = value;

			keys.forEach((key: string) => {
				tpl_d['key'] = key;
				value.push(name2title[key]);
				value.push(order[key]);
			});
			data.push(tpl_d);
		}
		if (Object.keys(param).length > 0) {
			const keys = Object.keys(param);
			keys.forEach((key: string) => {
				const tpl_d = {};
				tpl_d['key'] = key;
				tpl_d['field_name'] = name2title[key];
				tpl_d['field_value'] = param[key] || '空';
				data.push(tpl_d);
			});
		}

		render(fd, data, P01, 'P01', 'inner');
	}
}
